<?php
require_once 'data_form'.EXT;

class protection_against extends data_form{

	function __construct(){
		parent::__construct();
	}
	
	function index($type = 0)
	{
		//echo $type;
		if($type==0){
			$this->data['module_title']	=	'Protection Against Critical Illness';
		}
		else 
		{
			$this->data['module_title']	=	'Protection Against Disability';
		}
		$row	=	null;
		$this->load->model('protection_against_model');
		$this->load->model(array('existing_coverage_endowment_model','existing_coverage_term_model','existing_coverage_ilp_model','existing_coverage_wl_model','common_model'));
		$this->load->helper('custom_string');
		$this->data['endowments']	=	$this->existing_coverage_endowment_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['terms']	=	$this->existing_coverage_term_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['ilps']	=	$this->existing_coverage_ilp_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['wls']	=	$this->existing_coverage_wl_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$row	=	$this->protection_against_model->get_by(array('customer_id' => $_SESSION['customer']['id'],'type' => $type));
		$submit	=	$this->input->post('submit');
		if($_SERVER["REQUEST_METHOD"] == "GET")
		{
			if($row){
				$data	=	array(
					'customer_id'	=>	$row->customer_id,
					'yearly_income'		=>	$row->yearly_income,
					'percent_protection'	=>	$row->percent_protection,
					'number_year_protect'	=>	$row->number_year_protect,
					'lump_sum'	=>	$row->lump_sum,
					'type'		=>	$row->type,
					'explanation'	=>	$row->explanation
				);
				$_SESSION['data_'.$type]	=	$data;
			}else{
				//unset($_SESSION['data']);
			}
		}
		
		if($_SERVER["REQUEST_METHOD"]  == "POST")
		{
			$data_update	=	array(
					'customer_id'	=>	$_SESSION['customer']['id'],
					'yearly_income'		=>	$_POST['textfield5'],
					'percent_protection'	=>	$_POST['textfield2'],
					'number_year_protect'	=>	$_POST['textfield6'],
					'lump_sum'	=>	$_POST['textfield7'],
					'type'		=>	$type
					
				);
			if(isset($_POST['btnCalculate']) && $_POST['btnCalculate'] == 'Calculate'){
				if($row){
					$data_update['explanation']	=	$row->explanation;
				}
				$_SESSION['data_'.$type]	=	$data_update;
			}
			else{
				$id	=	null;
				//$data_update	=	$_SESSION['data_'.$type];
				//die($this->input->post('textfield3'));
				$data_update['explanation']	=	$this->input->post('textfield3');
				if($row)
				{
					//update data
					$this->protection_against_model->update($row->id,$data_update);
					$id	=	$row->id;
				}
				else
				{
					//insert new data
					$id	=	$this->protection_against_model->insert($data_update);
				}
				//print_r($_POST);
				//$row	=	$this->protection_against_model->get($id);
				redirect(base_url().'protection_against/index/'.$type);
			}
		}
		$this->data['type']	=	$type;
		$this->data['row']	=	(isset($_SESSION['data_'.$type]) ? $_SESSION['data_'.$type] : null);
		$this->load->view("protection_against/index", $this->data);
	}

}